<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use common\models\discount\FullCut;
use common\models\discount\FullCutSet;
/* @var $this yii\web\View */
/* @var $searchModel backend\models\FullCutSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '促销管理';
$this->params['breadcrumbs'][] = $this->title;

function getFullCutSet($id){
    $info = FullCutSet::find()
    ->where(['full_cut_id'=>$id])
    ->orderBy(['total_amount'=>SORT_DESC])
    ->all();
    $msg = '';
    if(!$info) {
        return '未设置';
    }
    foreach ($info as $val) {
        $msg .= '满'.$val->total_amount.'减'.$val->discount_amount."<br>";
    }
    
    return $msg;
}
?>
<div class="full-cut-index">

    <div class="box box-success">
        <div class="box-body"><?php echo $this->render('_search', ['model' => $searchModel]); ?></div>
    </div>
   <div class="box box-success">
   		<div class="box-body">
   	  <?php Pjax::begin(); ?>   
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,
        'columns' => [
           // ['class' => 'yii\grid\SerialColumn'],
            [
                'attribute'=>'full_cut_id',
                'value'=>function($data) {
                     return  $data->full_cut_id;
                }
            ],
            'title',
            [  
               'attribute'=>'c_obj',
               'value'=>function($data) {
                    return FullCut::$OBJ_DESC[$data->c_obj];
                 }
            ],
            [
                'attribute'=>'full_cut_set',
                'value'=>function($data) {
                    return getFullCutSet([$data->full_cut_id]);
                },
                'format'=>'html',
            ],
            /* [
                'attribute'=>'c_type',
                'value'=>function($data) {
                    return FullCut::$TYPE_DESC[$data->c_type];
                 }
            ], */
            'begin_time',
            'end_time',
            [
                'attribute'=>'c_status',
                'value'=>function($data) {
                  return FullCut::$STATUS_DESC[$data->c_status];
                }
            ],
             [
                'label'=>'状态',
                'value'=>function($data) {
                     $typeArr = [0=>'上线',1=>'下线'];
                    return Html::a($typeArr[$data->c_status],'javascript:;',['class' => 'change-status btn btn-primary btn-sm']);
                },
                'format'=>'html',
            ],
            ['class' => 'yii\grid\ActionColumn','template'=>'{update}'],
        ],
    ]); ?>
    <?php Pjax::end(); ?>
        </div>
    </div>
</div>
<?php $this->beginBlock('js');?>
<script>
$(function(){
	$(".change-status").click(function(){
		var _this = this;
		var id = $(_this).parent().parent().attr('data-key');
		var csrfToken = $('meta[name="csrf-token"]').attr("content");
		if(confirm('您确定执行当前操作吗？')){
			$.ajax({
                url: '<?=Yii::$app->urlManager->createUrl(Yii::$app->controller->id . '/change-status')?>',
                type: 'post',
                data: {id:id,_csrf:csrfToken},
                dataType: 'json',
                success: function (e) {
                   if(e.flag) {
                       var btn_name = e.c_status == 1 ? "下线" : "上线" ;
                       var text_name = e.c_status == 0 ? "下线" : "上线" ;
                       
                       $(_this).text(btn_name);
                       $(_this).parent().prev('td').text(text_name);
                       alert(e.msg);
                       return false;
                   }else {
                       alert(e.msg);
                       return false;
                   }
                }
            });
		}
	});
})
</script>
<?php $this->endBlock();?>
